/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.erp.server;

import com.erp.client.DAO.FileInfo;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.erp.client.DAO.ServerUserBaseGetService;
import com.erp.client.DAO.UserBaseInf;
import com.erp.client.systemEntryPoint;
import com.erp.server.DAO.FileDao;
import com.extjs.gxt.ui.client.widget.Info;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JFileChooser;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author Administrator
 */
public class ServletUpload extends HttpServlet {

    private int StaticEmpID;// = lls.get(0).getUserId();
    private int StaticDepId;//= lls.get(0).getDepId();
    private String StaticUserName;// = lls.get(0).getUserName();
    private String StaticDepName;//= lls.get(0).getDepName();
    private String uploadPath = "D:\\upload\\"; // 上传文件的目录
    private String tempPath = "D:\\upload\\tmp\\"; // 临时文件目录

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<UserBaseInf> userbaseinf = new ArrayList<UserBaseInf>();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        UserBaseInf tempuserbaseinf = new UserBaseInf();
        tempuserbaseinf = (UserBaseInf) session.getAttribute("empuser");
        userbaseinf.add(tempuserbaseinf);

        StaticUserName = userbaseinf.get(0).getUserName();
        StaticDepName = userbaseinf.get(0).getDepName();
        StaticEmpID = userbaseinf.get(0).getUserId();
        StaticDepId = userbaseinf.get(0).getDepId();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

        String uploadPath = "D:\\upload\\" + StaticDepName + "\\";

        if (!new File(uploadPath).isDirectory()) {
            new File(uploadPath).mkdirs();
        }
        if (!new File(tempPath).isDirectory()) {
            new File(tempPath).mkdirs();
        }
        System.out.println("########################");
        //FileItemFactory factory = new DiskFileItemFactory();
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // maximum size that will be stored in memory
        factory.setSizeThreshold(4096);
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = null;
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        Iterator it = items.iterator();

        List<FileInfo> listfb = new ArrayList<FileInfo>();
        JFileChooser chooser = new JFileChooser();
        FileDao insertmyfile = new FileDao();
        while (it.hasNext()) {
            FileItem item = (FileItem) it.next();
            //String filename = "d:/server/"+systemEntryPoint.StaticDepName+"/"+sdf.format(new Date())+systemEntryPoint.StaticDepName+systemEntryPoint.StaticUserName+ item.getName();
            if (item.getName().length() > 4) {
                FileInfo fb = new FileInfo();
                String filename = sdf.format(new Date()) + "-" + StaticUserName + "-" + StaticDepName + "-" + item.getName();
                File uploadedFile = new File(uploadPath + filename);//"d:/server/" + sdf.format(new Date()) + item.getName());
                try {
                    item.write(uploadedFile);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                fb.setEmpid(StaticEmpID);
                fb.setFileName(filename);
                fb.setMyfile(item.getName());
                fb.setDepName(StaticDepName);
                fb.setUserName(StaticUserName);
                fb.setFileType(chooser.getTypeDescription(uploadedFile));
                listfb.add(fb);
            }
        }
        try {
            insertmyfile.insertFileData(listfb);
        } catch (SQLException ex) {
            Logger.getLogger(ServletUpload.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}